package com.example.lenovo.findmyclass;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.lenovo.findmyclass.Buildings.BuildingFour;
import com.example.lenovo.findmyclass.Menu.Reservation.BuildingOneResevation.ReservationA1;
import com.example.lenovo.findmyclass.Menu.Reservation.ReservationClass;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Json extends Activity implements View.OnClickListener{
    Button response;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        response = (Button) findViewById(R.id.a1);
        response.setOnClickListener(this);

    }

    private void sendRequestWithOkHttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder().url("http://10.217.8.195/f_data.json").build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    parseJSONWithJSONObject(responseData);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
   /* private void parseJSONWithGSON(String jsonData){
        Gson gson = new Gson();
        List<App> appList = gson.fromJson(jsonData,new TypeToken<List<App>>(){}.getType());
        for (App app:appList){
            Log.d("MainActivity","id is " + app.getId());
            Log.d("MainActivity","name is " + app.getName());
            Log.d("MainActivity","version is " + app.getVersion());
        }
    }*/
    //使用JSONObject解析JSON数据
    private  void parseJSONWithJSONObject(String jsonData){
        try {
            JSONArray jsonArray = new JSONArray(jsonData);
            for (int i = 0;i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id = jsonObject.getString("id");
                String room = jsonObject.getString("room");
                String value = jsonObject.getString("value");
                Log.d("MainActivity","id is " + id);
                Log.d("MainActivity","room is " + room);
                Log.d("MainActivity","value is " + value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {Intent a1 = new Intent(Json.this,ReservationA1.class);
        startActivity(a1);
    }
    /*private void showResponse(final String response){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
//                在这里进行UI操作，将结果显示到界面上
                responseText.setText(response);
            }
        });
    }*/
}
